package rubik;

import javax.swing.JOptionPane;

public class Scrambler {

    public static void Scramble(int t, Cube cube) {
        String[] moves = {"U", "L", "F", "D", "R", "B", "U2", "L2", "F2", "D2",
            "R2", "B2", "U'", "L'", "F'", "D'", "R'", "B'"};
        int indexMove;
        int shifting = 0;
        StringBuilder buffer = new StringBuilder();
        buffer.append("Eseguo ").append(t).append(" mosse... ");
        int lastMove = -1;
        for (int i = 0; i < t; i++) {
            do {
                indexMove = (int) (Math.random() * 18);
            } while (indexMove == lastMove || indexMove == lastMove + 6 || indexMove == lastMove + 12
                    || indexMove == lastMove + shifting || indexMove == lastMove + 6 + shifting
                    || indexMove == lastMove + 12 + shifting);
            buffer.append(moves[indexMove]).append(" ");
            lastMove = indexMove;
            if (lastMove >= 12) {
                lastMove -= 12;
            } else if (lastMove >= 6) {
                lastMove -= 6;
            }

            if (lastMove >= 3) {
                shifting = -3;
            } else {
                shifting = +3;
            }

            if (moves[indexMove].charAt(0) == 'U') {
                if (moves[indexMove].length() == 1) {
                    cube.U();
                } else if (moves[indexMove].charAt(1) == '2') {
                    cube.U2();
                } else {
                    cube.Ua();
                }
            } else if (moves[indexMove].charAt(0) == 'D') {
                if (moves[indexMove].length() == 1) {
                    cube.D();
                } else if (moves[indexMove].charAt(1) == '2') {
                    cube.D2();
                } else {
                    cube.Da();
                }
            } else if (moves[indexMove].charAt(0) == 'F') {
                if (moves[indexMove].length() == 1) {
                    cube.F();
                } else if (moves[indexMove].charAt(1) == '2') {
                    cube.F2();
                } else {
                    cube.Fa();
                }
            } else if (moves[indexMove].charAt(0) == 'B') {
                if (moves[indexMove].length() == 1) {
                    cube.B();
                } else if (moves[indexMove].charAt(1) == '2') {
                    cube.B2();
                } else {
                    cube.Ba();
                }
            } else if (moves[indexMove].charAt(0) == 'L') {
                if (moves[indexMove].length() == 1) {
                    cube.L();
                } else if (moves[indexMove].charAt(1) == '2') {
                    cube.L2();
                } else {
                    cube.La();
                }
            } else if (moves[indexMove].charAt(0) == 'R') {
                if (moves[indexMove].length() == 1) {
                    cube.R();
                } else if (moves[indexMove].charAt(1) == '2') {
                    cube.R2();
                } else {
                    cube.Ra();
                }
            }
        }
        buffer.append("\n");//.append(cubo.getCubePic());
        cube.clearLastMove();
        cube.getCubeGraphics().forceUpdate(cube, true);
        JOptionPane.showMessageDialog(null, buffer, "Scrambled Cube", JOptionPane.PLAIN_MESSAGE);
    }
}